package com.fita.wechat.helper.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.fita.entity.data.Country;
import com.fita.entity.data.CountryCoupon;
import com.fita.entity.data.ExchangeRate;
import com.fita.entity.order.PayType;
import com.fita.entity.set.SetPayType;
import com.fita.wechat.helper.VehicleHelper;
import com.fita.wechat.service.CountryCouponService;
import com.fita.wechat.service.DataService;
import com.fita.wechat.service.ExchangeRateService;

/**
 * 
 * @author dan
 *
 */
@Component("vehiclePriceHelper")
public class VehiclePriceHelper implements VehicleHelper{
    
    @Autowired
    private ExchangeRateService exchangeRateService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Country> countryService;
    
    @Autowired
    private CountryCouponService countryCouponService;
    
    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.helper.VehicleHelper#getDestinationPaid(java.math.BigDecimal, com.fita.entity.order.PayType, java.lang.String, com.fita.entity.set.SetPayType)
     */
    public BigDecimal getDestinationPaid(BigDecimal totalAmount, PayType payType, String currencyCode, SetPayType setPayType) {
        
        BigDecimal destinationPaid = null;
        if(payType.isArrivepay()) {  // 到付
            destinationPaid = totalAmount;
            return destinationPaid;
        } else {                    // 预付
            ExchangeRate exchangeRate = exchangeRateService.load(currencyCode, new Date());
            if(setPayType.isDynamicPrepay()) {
                destinationPaid = totalAmount.multiply(new BigDecimal(1.176)).multiply(exchangeRate.getRate());
            } else {
                destinationPaid = totalAmount.multiply(exchangeRate.getRate());
            }
            return destinationPaid.setScale(0, RoundingMode.UP);
        }
    }

    /*
     * (non-Javadoc)
     * @see com.fita.wechat.helper.VehicleHelper#getDynamicPrepaid(java.math.BigDecimal, java.lang.String, int, com.fita.entity.set.SetPayType)
     */
    public BigDecimal getDynamicPrepaid(BigDecimal totalAmount, String currencyCode, int rate, SetPayType setPayType, Integer primaryAgentRate) {
        
        // 当前汇率
        ExchangeRate exchangeRate = exchangeRateService.load(currencyCode, new Date());
        //(1-返点率)
        BigDecimal currentRate = new BigDecimal(1).subtract(new BigDecimal(rate).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP));;
        
        BigDecimal prepaid = null;
        if(setPayType.isFloatPrepay()) {  // API*(1-返点率)
            prepaid = totalAmount.multiply(currentRate);
        } else if(setPayType.isDynamicPrepay()) { //API*117.6%*(1-返点率)
            prepaid = totalAmount.multiply(new BigDecimal(1.176)).multiply(currentRate);
        }
        prepaid = prepaid.multiply(exchangeRate.getRate()).setScale(0, RoundingMode.UP);

        //二级代理价格增加提成
        if (primaryAgentRate != null){
            prepaid = prepaid.add(prepaid.multiply(new BigDecimal(primaryAgentRate).divide(new BigDecimal(100))).setScale(0, RoundingMode.UP));
        }

        return prepaid.setScale(0, RoundingMode.UP);
    }

    /*
     * (non-Javadoc)
     * @see com.fita.wechat.helper.VehicleHelper#getPrepaid(java.math.BigDecimal, java.lang.String, int)
     */
    public BigDecimal getPrepaid(BigDecimal totalAmount, String currencyCode, int rate, Integer primaryAgentRate) {
        
        BigDecimal dynamicPrepaid = null;
        
        // 1-返点率
        BigDecimal currentRate = new BigDecimal(1).subtract(new BigDecimal(rate).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP));
        // 当前汇率
        ExchangeRate exchangeRate = exchangeRateService.load(currencyCode, new Date());


        // API*125%*(1-返点率)*汇率
        dynamicPrepaid = totalAmount.multiply(new BigDecimal(1.25)).multiply(currentRate).multiply(exchangeRate.getRate()).setScale(0, RoundingMode.UP);

        //二级代理价格增加提成
        if (primaryAgentRate != null){
            dynamicPrepaid = dynamicPrepaid.add(dynamicPrepaid.multiply(new BigDecimal(primaryAgentRate).divide(new BigDecimal(100))));
        }

        return dynamicPrepaid.setScale(0, RoundingMode.UP);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.helper.VehicleHelper#getAgentCommission(java.math.BigDecimal, com.fita.entity.data.Country, int)
     */
    public BigDecimal getAgentCommission(BigDecimal totalAmount, Country country, int rate, Integer primaryAgentRate) {
        CountryCoupon coupon = countryCouponService.load(country, new Date());
        if(coupon == null) {
            BigDecimal agentCommission = totalAmount.multiply(new BigDecimal(rate).divide(new BigDecimal(100)));
            if (primaryAgentRate != null && primaryAgentRate > 0){
                BigDecimal primaryAgentRateBigDecimal = new BigDecimal(primaryAgentRate).divide(new BigDecimal(100));
                primaryAgentRateBigDecimal = new BigDecimal(1).subtract(primaryAgentRateBigDecimal);
                agentCommission = agentCommission.multiply(primaryAgentRateBigDecimal);
            }
            return agentCommission.setScale(2, BigDecimal.ROUND_HALF_UP);
        } else {
            int currentRate = 10000-(100-rate)*coupon.getRate();
            return totalAmount.multiply(new BigDecimal(currentRate).divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
        }
    }

}
